﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum EScenesType
{
    EST_None,
    EST_Login,
    EST_Play,
}

public enum EWindowType
{
    EWT_LoginWindow, //登录
    EWT_RoleWindow, //用户
    EWT_PlayWindow,//战斗
}

public class WindowManager : MonoBehaviour {

    private Dictionary<EWindowType, BaseWindow> mWidowDic;

    public WindowManager()
    {
        mWidowDic = new Dictionary<EWindowType, BaseWindow>();

        mWidowDic[EWindowType.EWT_LoginWindow] = new LoginWindow();

        //mWidowDic[EWindowType.EWT_RoleWindow] = new RoleWindow();
        //mWidowDic[EWindowType.EWT_PlayWindow] = new PlayWindow();
    }

    public void ChangeScenseToPlay(EScenesType front)
    {
        foreach (BaseWindow pWindow in mWidowDic.Values)
        {
            if (pWindow.GetScenseType() == EScenesType.EST_Play)
            {
                pWindow.Init();

                if (pWindow.IsResident())
                {
                    pWindow.PreLoad();
                }
            }
            else if ((pWindow.GetScenseType() == EScenesType.EST_Login) && (front == EScenesType.EST_Login))
            {
                pWindow.Hide();
                pWindow.Realse();

                if (pWindow.IsResident())
                {
                    pWindow.DelayDestory();
                }
            }
        }
    }
    //切换到登录场景
    public void ChangeScenseToLogin(EScenesType front)
    {
        foreach (BaseWindow pWindow in mWidowDic.Values)
        {
            if (front == EScenesType.EST_None && pWindow.GetScenseType() == EScenesType.EST_None)
            {
                pWindow.Init();

                if (pWindow.IsResident())
                {
                    pWindow.PreLoad();
                }
            }

            if (pWindow.GetScenseType() == EScenesType.EST_Login)
            {
                pWindow.Init();

                if (pWindow.IsResident())
                {
                    pWindow.PreLoad();
                }
            }
            else if ((pWindow.GetScenseType() == EScenesType.EST_Play) && (front == EScenesType.EST_Play))
            {
                pWindow.Hide();
                pWindow.Realse();

                if (pWindow.IsResident())
                {
                    pWindow.DelayDestory();
                }
            }
        }
    }

    public void HideAllWindow(EScenesType front)
    {
        foreach (var item in mWidowDic)
        {
            if (front == item.Value.GetScenseType())
            {
                Debug.Log(item.Key);
                item.Value.Hide();
                //item.Value.Realse();
            }
        }
    }

    public void Update(float deltaTime)
    {
        foreach (BaseWindow pWindow in mWidowDic.Values)
        {
            if (pWindow.IsVisible())
            {
                pWindow.Update(deltaTime);
            }
        }
    }
}
